using Clancy.Data;
using Clancy.Interfaces;
using Clancy.Service.Client.ClancyServiceClient;
using System.Linq;

namespace Clancy.Service.Client
{
    public class ClancyCommunicationCallbackService : IClancyCommunicationServiceCallback
    {
        private readonly IOperationAdapter m_OperationsAdapter;
        private ISurveilanceStorage m_Storage;

        public ClancyCommunicationCallbackService(IOperationAdapter operationsAdapter, ISurveilanceStorage storage)
        {
            m_OperationsAdapter = operationsAdapter;
            m_Storage = storage;
        }

        public void ReturnCurrentSurveilances(SurveilanceObject[] surveilanceObjects)
        {
            m_Storage.SurveilanceObjects.Clear();
            ReturnSurveilancesAdded(surveilanceObjects);
        }

        public void ReturnSurveilanceResultFrame(SurveilanceObject surveilanceObject, SurveilanceResultFrame resultFrame)
        {
        }

        public void ReturnSurveilancesAdded(SurveilanceObject[] surveilanceObjects)
        {
            foreach (SurveilanceObject surveilanceObject in surveilanceObjects)
            {
                m_Storage.SurveilanceObjects.Add(surveilanceObject);
            }
        }

        public void ReturnSurveilancesRemoved(SurveilanceObject[] surveilanceObjects)
        {
            foreach (SurveilanceObject surveilanceObject in surveilanceObjects)
            {
                ISurveilanceObject objectToDelete = m_Storage.SurveilanceObjects.FirstOrDefault(survObj => survObj.Id == surveilanceObject.Id);
                if (objectToDelete == null)
                    continue;

                m_Storage.SurveilanceObjects.Remove(objectToDelete);
            }
        }
    }
}